package corp.edu.amadeus.web.action;

import com.opensymphony.xwork2.ActionSupport;
import corp.edu.amadeus.core.business.locator.ServiceLocator;
import corp.edu.amadeus.core.business.object.impl.TextBO;
import corp.edu.amadeus.core.business.service.ifc.ITextService;
import java.util.Collection;
import org.apache.log4j.Logger;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.StringLengthFieldValidator;

public class TextAction extends ActionSupport  {

	private static Logger log = Logger.getLogger(TextAction.class);
	private Collection<TextBO> textsList;
	private Integer id;
	private String text;
	private String lang;
	private ITextService textService;


	public TextAction() {
		this.textService = ServiceLocator.getTextService();
		this.textsList = this.textService.getAllTexts();
	}

	public String execute() throws Exception {
		this.textsList = this.textService.getAllTexts();
    	return SUCCESS;
    }

	public String save() throws Exception {
		log.debug("Saving text data.");
		if(this.id!=null) {
			this.textService.updateText(this.id, this.text, this.lang);
		} else {
			this.textService.addText(this.text, this.lang);
		}
		return this.execute();
	}

	public String list() throws Exception {
		return this.execute();
	}

	public String remove() throws Exception {
		return this.execute();
	}

	@StringLengthFieldValidator(trim = true, minLength="1" ,maxLength = "1000", message = "Max:1000 - Min:1")
	public void setText(String t) { this.text = t; }
	public String getText() { return this.text; }

	@RequiredStringValidator(message = "Por favor, introduzca el idioma", trim = true)
	public void setLang(String l) { this.lang = l; }
	public String getLang() { return this.lang; }

	public Integer getId() { return this.id; }
	public void setId(Integer i) { this.id = i; }

	public void setTextsList(Collection<TextBO> l) { this.textsList = l;}
	public Collection<TextBO> getTextsList() { return this.textsList; }

}